package com.zisuyi.uilibrary.base

import com.weilele.mvvm.base.helper.*
import com.weilele.mvvm.utils.activity.toast
import com.zisuyi.game.sdk.base.BaseViewModel
import com.zisuyi.game.sdk.net.bean.BaseGameBean
import com.zisuyi.game.sdk.net.bean.SmallGameRewardBean
import com.zisuyi.game.sdk.utils.UserData
import com.zisuyi.uilibrary.view.AmountTextView
import com.zisuyi.uilibrary.view.CoinTextView

abstract class BaseUiViewModel : BaseViewModel() {
    companion object {
        val coinViews = mutableListOf<CoinTextView>()
        val amountViews = mutableListOf<AmountTextView>()
    }

    /**
     * retrofit请求，添加统一的结果回调
     */
    protected fun <T> StatusLiveData<T>.retrofitGame(
        isToastError: Boolean = true/*是否toast出错误*/,
        isKeepLastValue: Boolean = false/*是否保留之前已经赋值的数据*/,
        errorCall: ((Throwable) -> BaseGameBean<T>?)? = null,
        call: suspend () -> BaseGameBean<T>?
    ) {
        val liveData = this
        liveData.running(isKeepLastValue)
        launchWithScope {
            withError(
                errorCall = {//执行call发生错误
                    if (errorCall == null) {
                        val errorMsg = it.message ?: "createOkHttpCall Error"
                        if (isToastError) {
                            errorMsg.toast(android.widget.Toast.LENGTH_LONG)
                        }
                        liveData.error(errorMsg, isKeepLastValue, null, null)
                        null
                    } else {
                        errorCall.invoke(it)
                    }
                },
                resultCall = {//执行call得到结果
                    if (it?.code == 200) {
                        if (it.assets != null) {
                            coinViews.toList().forEach { view ->
                                view.setNumber(it.assets?.coin ?: 0)
                            }
                            amountViews.toList().forEach { view ->
                                view.setNumber(it.assets?.diamond ?: 0)
                            }
                            it.assets.isEnableSendToJs = true
                            UserData.userAssetsData.success(it.assets)
                        }
                        liveData.success(it.data, isKeepLastValue)
                    } else {
                        val errorMsg = it?.message ?: "result is null"
                        if (isToastError) {
                            errorMsg.toast(android.widget.Toast.LENGTH_LONG)
                        }
                        liveData.error(errorMsg, isKeepLastValue)
                    }
                },
                call = { call.invoke() }
            )
        }
    }

    protected fun StatusLiveData<SmallGameRewardBean>.retrofitGameReward(
        isToastError: Boolean = true/*是否toast出错误*/,
        isKeepLastValue: Boolean = false/*是否保留之前已经赋值的数据*/,
        errorCall: ((Throwable) -> BaseGameBean<*>?)? = null,
        call: suspend () -> BaseGameBean<*>?
    ) {
        val liveData = this
        liveData.running(isKeepLastValue)
        launchWithScope {
            withError(
                errorCall = {//执行call发生错误
                    if (errorCall == null) {
                        val errorMsg = it.message ?: "createOkHttpCall Error"
                        if (isToastError) {
                            errorMsg.toast(android.widget.Toast.LENGTH_LONG)
                        }
                        liveData.error(errorMsg, isKeepLastValue, null, null)
                        null
                    } else {
                        errorCall.invoke(it)
                    }
                },
                resultCall = {//执行call得到结果
                    if (it?.code == 200) {
                        if (it.assets != null) {
                            coinViews.toList().forEach { view ->
                                view.setNumber(it.assets?.coin ?: 0)
                            }
                            amountViews.toList().forEach { view ->
                                view.setNumber(it.assets?.diamond ?: 0)
                            }
                            it.assets.isEnableSendToJs = true
                            UserData.userAssetsData.success(it.assets)
                        }
                        liveData.success(it.reward, isKeepLastValue)
                    } else {
                        val errorMsg = it?.message ?: "result is null"
                        if (isToastError) {
                            errorMsg.toast(android.widget.Toast.LENGTH_LONG)
                        }
                        liveData.error(errorMsg, isKeepLastValue)
                    }
                },
                call = { call.invoke() }
            )
        }
    }
}